<?php
ob_start();
session_start();
include("utils.php");

main_content("mainBuySpace();");
function mainBuySpace()
{
    if($_SESSION['langId']!=""){
            $langId=$_SESSION['langId'];
    }else{
            $langId="1";
    }

    $sql="select s.*,adm.*,addet.adspaceType FROM ".SITEMASTER." as s,".ADSPSCEMASTER." as adm,".ADSPACEDETAILS." as addet where s.langId='".$langId."' and s.adsiteId=adm.adsiteId and addet.adspaceId=adm.adspaceId and adm.ad_Type='direct' AND s.adSiteType<>''";
    $query=mysql_query($sql);
    if(isset($_SESSION[siteuserId]) && $_SESSION[siteuserId]!='')
    $callFunction = "addtoFav();";
    else
    $callFunction = "location.href='start.php'";
?>
<script type="text/javascript">
function addtoFav(){
    if(document.getElementById('hidAdSpaceId').value==''){
            document.getElementById('errorId').innerHTML = '<div style="width:750px;height:40px;border:#999999 thin solid; vertical-align:middle;" class="err_class"><br><?php echo SELECT_RECORD; ?>.</div>';
    }
    else{
            document.getElementById('errorId').innerHTML = '';
            adtoFavAjax('include_ajax/',document.getElementById('hidAdSpaceId').value);
    }
}
function addAdSpaceId(val,$count){
    hidID = document.getElementById('hidAdSpaceId').value;
    var valWithComma = val+',';
    if(hidID.search(val)==-1){
        document.getElementById('hidAdSpaceId').value =valWithComma+document.getElementById('hidAdSpaceId').value;
        document.getElementById('trList'+val).className = 'normalActive';
    }
    else{
        document.getElementById('hidAdSpaceId').value =hidID.replace(valWithComma,'');
        if($count==0)
        document.getElementById('trList'+val).className = 'normal2';
        else
        document.getElementById('trList'+val).className = 'normal';
    }
}
function checkClassName(val,$count){
        hidID = document.getElementById('hidAdSpaceId').value;
        var valWithComma = val+',';
        if(hidID.search(val)==-1){
            if($count==0)
                document.getElementById('trList'+val).className = 'normal2';
            else
                document.getElementById('trList'+val).className = 'normal';
        }
        else{
            if($count==0)
                document.getElementById('trList'+val).className = 'normalActive2';
            else
                document.getElementById('trList'+val).className = 'normalActive';
        }
}
</script>
<link rel="stylesheet" href="popup_js/style.css" type="text/css" />
<link rel="stylesheet" href="popup_js/style_002.css" type="text/css" />

<script type="text/javascript" src="popup_js/popup-layer.js"></script>
<script type="text/javascript" src="popup_js/popup-adspace.js"></script>
<script	type="text/javascript" src="popup_js/popup-price.js"></script>
<script type="text/javascript" src="ajax/prototype.js"></script>
<script	type="text/javascript" src="ajax/ajax.js"></script>
<script language="javascript" type="text/javascript">
var delayTimeForMouseoverPopup = 500;
var sid = 'f24dc7bb351fe6c1ed714721b1e632ff';
var popUpLayer = new PopUpLayer(true);
var randomNumber = null;
var disableDisplay = window.onload;
window.onload = function(){
    if (disableDisplay){
        disableDisplay();
    }
}
function popUpPreview(id, e, layer){
    randomNumber = Math.random();
    var element = PopUpLayer.getSrcElement(e);
    var position = PopUpLayer.getCoordinate(element);
    window.setTimeout(popUpImpl, delayTimeForMouseoverPopup, id, position, element, layer, randomNumber);
}

function popUpImpl(id, position, element, layer, number) {
    if (number != randomNumber) {
        return null;
    }
    if (layer == 'product') {
        popupSpace.show(id);
        popupSpace.adjustPopUpPosition(position, element);
    }
    else {
        popupPrice.show(id);
        popupPrice.adjustPopUpPosition(position, element);
    }
}

var _st = window.setTimeout;
window.setTimeout = function(fRef, mDelay) {
    if(typeof fRef == 'function'){
        var argu = Array.prototype.slice.call(arguments,2);
        var f = (function(){ fRef.apply(null, argu); });
        return _st(f, mDelay);
    }
    return _st(fRef,mDelay);
}   

function hidePreview() {
    randomNumber = Math.random();
    if (true == popupSpace.isDisplay) {
        popupSpace.hide();
    }
    if (true == popupPrice.isDisplay) {
        popupPrice.hide();
    }
}

//Resize page when the control is wider than the table.
function resizePage() {
    var width = document.getElementById("spaceListBlock").scrollWidth;
    document.getElementById("control").style.width = parseInt(width) + "px";
}
</script>
<input type="hidden" name="hidAdSpaceId" id="hidAdSpaceId" value="">
<div style="padding-bottom: 15px;">
<table width="90%" border="0" cellspacing="0" cellpadding="0" align="center">
    <tr>
        <td valign="top">
        <table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">
        <tr>
            <td width="20%" valign="top"><?php include("left_pannel.php"); ?></td>
            <td style="width: 10px;">&nbsp;</td>
            <td valign="top">
                <table width="100%" border="0" cellspacing="0" cellpadding="0">
                <tr>
                    <td valign="top">
                    <table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">
                    <tr>
                        <td class="BoxContent">
                        <table width="100%" class="center" cellspacing="0" cellpadding="0" id="spaceListBlock">
                        <tr>
                            <td><img src="images/red_table_ltop.jpg" alt="" /></td>
                            <td class="red_table_top"></td>
                            <td><img src="images/red_table_rtop.jpg" alt="" /></td>
                        </tr>
                        <tr>
                            <td class="red_table_LSide"></td>
                            <!-- ban body part -->
                            <td align="center">
                            <table width="95%" cellspacing="0" cellpadding="0" border="0">
                            <tr>
                                <td align="left"><a href="buyNetworkAds.php"><?php echo BY_NETWORK_ADS; ?></a></td>
                            </tr>
                            <tr>
                                <td align="center" class="sky font-18"><?php echo AD_MARKET_PLACE; ?></td>
                            </tr>
                            <tr>
                                <td align="right"><input type="button" value="<?php echo AD_TO_FAVOURITES; ?>" id="btnAddToFav" onclick="<?php echo $callFunction;?>"></td>
                            </tr>
                            <tr>
                                <td align="center" id="errorId">&nbsp;</td>
                            </tr>
                            <tr>
                                <td>
                                <table width="100%" class="mainTable" cellspacing="0" cellpadding="0">
                                <tr>
                                    <td class="sitename"><a href="#" class="headerLink"><?php echo SITE_NAME; ?></a></td>
                                    <td class="sitename"><a href="#" class="headerLink">Type</a></td>
                                    <td class="product"><a href="#" class="headerLink"><?php echo PRODUCT; ?></a></td>
                                    <td class="rank"><a href="#" class="headerLink"><?php echo RANK; ?></a></td>
                                    <td class="impression"><a href="#" class="headerLink"><?php echo IMPRESSION; ?></a></td>
                                    <td class="click"><a href="#" class="headerLink"><?php echo CLICK_DAY; ?></a>&nbsp;<img src="images/dwn_arrow.jpg" alt="" /></td>
                                    <td class="cpc"><a href="#" class="headerLink"><?php echo CPC; ?></a></td>
                                    <td class="price"><a href="#" class="headerLink"><?php echo MIN_PRICE_DAY; ?></a></td>
                                </tr>
                                <?php
                                    $i=0;
                                    $GlobaLSPAce = '';
                                    $GlobaLPRIce = '';
                                    while($result= mysql_fetch_array($query)){
                                        $i++;
                                        if($i%2==0)
                                        {
                                                $className = 'normalActive2';
                                                $className2 = 'normal2';
                                                $colorClass = 'gray';
                                        }
                                        else
                                        {
                                                $className = 'normalActive';
                                                $className2 = 'normal';
                                                $colorClass = 'white';
                                        }
                                        $siteUrlReplaced = str_replace("http://","",$result[adSiteUrl]);
                                        $siteUrlReplaceWWW = str_replace("www.","",$siteUrlReplaced);
                                        if($result[adspaceType]=='banner')
                                        $imgName = 'ban_icon.jpg';
                                        elseif($result[adspaceType]=='thumbnail')
                                        $imgName = 'thum_icon.jpg';
                                        else
                                        $imgName = 'tt_icon.gif';

                                ?>
                                <tr id="trList<?php echo $result[adspaceId]; ?>"
                                        onmouseover="this.className='<?php echo $className;?>'"
                                        onmouseout="checkClassName(<?php echo $result[adspaceId]; ?>,<?php echo ($i%2); ?>);"
                                        class="<?php echo $className2; ?>" onclick="addAdSpaceId(<?php echo $result[adspaceId]; ?>,<?php echo ($i%2); ?>);">
                                    <td class="<?php echo $colorClass;?>_sitename"><a href="javascript:void(0);" onclick="location.href='adSpaceDetails.php?tab=buy_ads&subMenu=1&id=<?php echo $result[adspaceId];?>&site_type=<?php echo $result[adSiteType]?>'" class="listLink"><?php echo $siteUrlReplaceWWW; ?></a></td>
                                    <td class="<?php echo $colorClass;?>_rank"><a href="javascript:void(0);" onclick="location.href='adSpaceDetails.php?id=<?php echo $result[adspaceId];?>&site_type=<?php echo $result[adSiteType]?>'" class="listLink"><?php echo $result[adSiteType]; ?></a></td>
                                    <td class="<?php echo $colorClass;?>_product"><a href="#" class="listLink" onmouseout="hidePreview();"
                                        onmouseover="popUpPreview(<?php echo $result[adspaceId]; ?>, event, 'product');" id="balloon<?php echo $result[adspaceId]; ?>">
                                        <img src="images/<?php echo $imgName;?>" border="0" alt="" />&nbsp;<?php echo ucfirst($result[adspaceType]); ?>
                                        </a>
                                    </td>
                                    <td class="<?php echo $colorClass;?>_rank"><?php echo getAlexaRank($siteUrlReplaced); ?></td>
                                    <td class="<?php echo $colorClass;?>_impression"><?php echo $result[adSiteImpression]; ?></td>
                                    <td class="<?php echo $colorClass;?>_click"><?php echo $result[clickPerDay]; ?></td>
                                    <td class="<?php echo $colorClass;?>_cpc">$<?php  echo number_format($result[costPerClick], 2, '.', ''); ?></td>
                                    <td class="<?php echo $colorClass;?>_price"><a href="#"
                                    class="listLink" onmouseout="hidePreview();"
                                    onmouseover="popUpPreview(<?php echo $result[adspaceId]; ?>, event, 'price')">$<?php if(empty($result[minPrice])){echo "0.00";} else { echo  $result[minPrice];} ?></a>
                                    <div style="display: none; width: 400px; z-index: 1; position: absolute; background-color: #ffffff; border: #CCCCCC 1px solid;"
                                        id="adpriceDiv<?php echo $result[adspaceId]; ?>">
                                    </div>
                                    </td>
                                </tr>
                                <?php
                                $sql1="select * FROM ".ADSPSCEMASTER." where langId='".$langId."' and adspaceId='".$result[adspaceId]."'";
                                $query1=mysql_query($sql1);
                                $result1= mysql_fetch_array($query1);
                                $explodeDate1 = explode(' ' ,$result[publishDate]);
                                $siteUrlReplaced1 = str_replace("http://","",$result1[adSiteUrl]);
                                $siteUrlReplaceWWW1 = str_replace("www.","",$siteUrlReplaced1);

                                $sqlPrice="select * FROM ".PRICE." where adspaceId='".$result[adspaceId]."' order by duration asc";
                                $queryPrice=mysql_query($sqlPrice);
                                if($result[adSiteType]=='WEB')
                                    $adSiteType = 'Website';
                                else
                                    $adSiteType = 'Mobile';
                                    #<!--###############################################################################################-->
                                    $GlobaLSPAce .= "spaceList[" . $result[adspaceId] . "] = ({
                                        'title' : '".$result1[adSpaceName]."',
                                        'image' : '".$GLOBALS[SiteUrl]."upload_adspace_image_thumb/".$result1['adSpace_image']."',
                                                     'theme' : '',
                                        'link' : 'prompt.php?sid=f24dc7bb351fe6c1ed714721b1e632ff&url=http%3A%2F%2Fwww%2Eddlspot%2Ecom',
                                        'type' : 'GTB',
                                        'categoryPathShort' : '".$adSiteType."',
                                        'siteRating' : '".$result1[adsSiteRating]."',
                                        'urlDisplay' : '".$result1[adSiteUrl]."',
                                        'availableDate' : '".$explodeDate1[0]."',
                                                    'demographicNumber' : '6',
                                        'demographicDisplay' : new Array(),
                                        'siteDescription' : '".$result[siteDescription]."',
                                        'spaceDescription' : '".substr($result1[description],0,200)."',
                                        'spaceTags' : '".$result1[tags]."'
                                    });";
                                    $execTopCountry = mysql_query("select noOfClicks,adCountryName from adspaceTopcountries where adspaceId='".$result[adspaceId]."' order by noOfClicks desc limit 0,5");
                                    $GlobaLPRIce .= "priceList[" . $result[adspaceId] . "] = new Array();";
                                    if(mysql_num_rows($queryPrice)>0){
                                        while($resultPrice= mysql_fetch_array($queryPrice)){
                                            if($result[clickPerDay]==0){
                                                $clickPerDay = '*New';
                                                $avgCPC = '*New';
                                            }
                                            else{
                                                $clickPerDay = $result[clickPerDay]*$resultPrice[duration];
                                                $avgCPC = '$'.number_format($resultPrice[ammount]/$result[clickPerDay], 2, '.', '');
                                            }
                                            $GlobaLPRIce .= "priceList[" . $result[adspaceId] . "].push({
                                                'day' : '".$resultPrice[duration]."',
                                                'price' : '$".$resultPrice[ammount]."',
                                                'avgClicks' : '".$clickPerDay."',
                                                'avgCostPerClick' : '".$avgCPC."'
                                            }); ";
                                        }
                                    }
                                    else{
                                        $GlobaLPRIce .= "priceList[" . $result[adspaceId] . "].push({
                                        'day' : 'No',
                                        'price' : ' Found',
                                        'avgClicks' : ' Record',
                                        'avgCostPerClick' : ''
                                        }); ";
                                    }
                                }
                                #<!--###############################################################################################-->
?>                              </table>
                                </td>
                            </tr>
                            </table>
                            </td>
                            <!-- /ban body part -->
                            <td class="red_table_RSide"></td>
                        </tr>
                        <tr>
                        <td><img src="images/red_table_lbottom.jpg" alt="" /></td>
                        <td class="red_table_bottom"></td>
                        <td><img src="images/red_table_rbottom.jpg" alt="" /></td>
                        </tr>
                        <div id="errorBlock" style="width: 450px; position: absolute; display: none;">
                            <table style="background-color: rgb(255, 255, 255); width: 100%;" cellpadding="3" cellspacing="1">
                            <tbody>
                            <tr>
                                <td style="text-align: left; color: rgb(255, 255, 255); font-weight: bold;" class="blue-background-small">Error,The ad space does not
                                exist.
                                </td>
                            </tr>
                            </tbody>
                            </table>
                        </div>
                        <div id="spacePreviewBlock" style="width: 450px; position: absolute; display: none; border: 1px #cccccc solid; z-index: 80; top: 208px; left: 681px;">
                            <table style="background-color: #ffffff; width: 100%;" cellpadding="3" cellspacing="1">
                            <tbody>
                            <tr>
                                <td class="listHeader" id="title"></td>
                            </tr>
                            <tr>
                                <td>
                                <div style="padding: 3px; text-align: left;">
                                    <table id="ptable" class="login_txt" cellpadding="2"
                                            cellspacing="2">
                                            <tbody>
                                            <tr>
                                                <td style="padding-top: 10px;">
                                                <div align="center"><strong>Available:</strong><span
                                                        id="availabledate" style="color: rgb(0, 0, 0);"></span></div>
                                                <div align="center"><a id="imageLink" href=""
                                                        target="_blank"><img src="image/noimage.jpg"
                                                        style="border: 4px none rgb(153, 153, 153); width: 201px; height: 147px;"
                                                        alt="label_text_space_image"></a></div>
                                                <div id="categoryPath"
                                                        style="padding: 2px; background: rgb(225, 225, 225) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;"
                                                        title="Technology &amp;amp; Internet" align="center"></div>
                                                </td>
                                                <td style="vertical-align: top; padding-top: 0px;">
                                                <table cellpadding="2" cellspacing="2" class="login_txt">
                                                <tbody>
                                                <tr id="lastImpressionBlock" style="display: none;">
                                                    <td class="highlight-label"
                                                            style="border-right-style: none;"><span
                                                            class="blackbold">Last Impression:</span>&nbsp;<span
                                                            id="lastImpression"
                                                            style="font-weight: lighter; display: block;">N/A</span><span
                                                            style="font-weight: lighter; display: none;"></span></td>
                                                </tr>
                                                <tr id="themeBlock" style="display: none;">
                                                    <td><span class="blackbold"
                                                            style="padding-right: 2px;"></span><span id="theme"></span></td>
                                                </tr>
                                                <tr>
                                                    <th nowrap="nowrap" align="left">Top 3 countries:</th>
                                                </tr>
                                                <tr id="topCountry" style="display: block;">
                                                    <td>N/A</td>
                                                </tr>
                                                <tr>
                                                    <td id="topCountry1" align="left"></td>
                                                </tr>
                                                <tr>
                                                    <td id="topCountry2" align="left"></td>
                                                </tr>
                                                <tr>
                                                    <td id="topCountry3" align="left"></td>
                                                </tr>
                                                <tr>
                                                    <td nowrap="nowrap" align="left"><span><strong>Site
                                                    Rating:</strong></span><span id="siteRating"></span></td>
                                                </tr>
                                                <tr>
                                                    <td nowrap="nowrap"><span id="urlDisplay"
                                                            class="blackbold"></span></td>
                                                </tr>
                                                </tbody>
                                                </table>
                                                </td>
                                            </tr>
                                            </tbody>
                                    </table>
                                <hr>
                                <table style="text-align: left;" class="login_txt">
                                <tbody>
                                <tr>
                                    <th><strong>Site Description:</strong></th>
                                </tr>
                                <tr>
                                    <td id="siteDescription"></td>
                                </tr>
                                </tbody>
                                </table>
                                <hr>
                                <table style="text-align: left;" cellspacing="4" class="login_txt">
                                <tbody>
                                <tr>
                                    <td style="width: 300px;"><strong>Ad Space Description:</strong></td>
                                    <td style="width: 250px;"><strong>Tags:</strong></td>
                                </tr>
                                <tr>
                                    <td id="spaceDescription" style="vertical-align: top;" align="justify"></td>
                                    <td id="spaceTags" style="vertical-align: top;"></td>
                                </tr>
                                </tbody>
                                </table>
                                </div>
                                </td>
                            </tr>
                            </tbody>
                            </table>
                        </div>
                        <div id="pricePreviewBlock" style="padding: 1px; width: 300px; background-color: #ffffff; display: none; position: absolute; z-index: 80;">
                            <div>
                            <table id="adspacePricing"
                                    style="width: 100%; border: 1px #cccccc solid;"
                                    class="login_txt" cellpadding="0" cellspacing="1">
                            <tr align="center">
                                <td class="listHeader">Duration (Days)</td>
                                <td class="listHeader">Avg. Clicks</td>
                                <td class="listHeader">Price</td>
                                <td class="listHeader">Avg. CPC</td>
                            </tr>
                            </table>
                            </div>
                        </div>
                        <script type="text/javascript">
                        var spaceList = new Array();
                        var popupSpace = new PopUpAdspace();
                        var initSpaceData = window.onload;
                        window.onload = function() {
                            if (initSpaceData) {
                                initSpaceData();
                            }
                            <?php echo $GlobaLSPAce;?>
                            popupSpace.setData(spaceList);
                        }
                        /*******************************************************************************************/
                        var priceList = new Array();
                        var popupPrice = new popUpPrice();
                        var initSpacePrice = window.onload;
                        window.onload = function() {
                            if(initSpacePrice) {
                                initSpacePrice();
                            }
                            <?php echo $GlobaLPRIce;?>
                            popupPrice.setData(priceList);
                        }
                        </script>
                        </table>
                        </td>
                    </tr>
                    </table>
                    </td>
                </tr>
                </table>
            </td>
        </tr>
        <tr>
            <td>&nbsp;</td>
        </tr>
        </table>
        </td>
        <td>&nbsp;</td>
    </tr>
</table>
</div>
<?php
}
?>
